package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Drugs;
import com.ruoyi.system.service.IDrugsService;
import com.ruoyi.system.service.impl.ConstantItemServiceImpl;
import com.ruoyi.web.controller.tool.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 药品信息Controller
 *
 * @author ruoyi
 * @date 2023-07-04
 */
@Api(tags = "药品信息模块")
@RestController
@RequestMapping("/system/Drugs")
public class DrugsController extends BaseController {
    @Autowired
    private IDrugsService drugsService;
    private class Wrap implements Serializable {
        Drugs Drugs;
        String DrugsDosage;
        String DrugsType;
        @JsonProperty("Drugs")
        public com.ruoyi.system.domain.Drugs getDrugs() {
            return Drugs;
        }
        @JsonProperty("DrugsDosage")
        public String getDrugsDosage() {
            return DrugsDosage;
        }
        @JsonProperty("DrugsType")
        public String getDrugsType() {
            return DrugsType;
        }
    }
    @Autowired
    ConstantItemServiceImpl constantItemService;

    private Wrap convert(Drugs origin) {
        Wrap tempWrap = new Wrap();
        tempWrap.DrugsDosage=constantItemService.getById(origin.getDrugsDosage()).getConstantName();
        tempWrap.DrugsType=constantItemService.getById(origin.getDrugsType()).getConstantName();
        tempWrap.Drugs = origin;
        return tempWrap;
    }
    /**
     * 查询药品信息列表
     */
    @ApiOperation(value = "查询药品信息列表")
    @PreAuthorize("@ss.hasPermi('system:Drugs:list')")
    @GetMapping("/list")
    public Page<Wrap> list(Drugs entity, Page<Drugs> page) {
        QueryWrapper<Drugs> wrapper = new QueryWrapper<>();
        if (entity.getDrugsCode() != null) {
            wrapper.like("DrugsCode", entity.getDrugsCode());
        }
        if (entity.getMnemonicCode() != null) {
            wrapper.like("MnemonicCode", entity.getMnemonicCode());
        }
        if (entity.getDrugsName() != null) {
            wrapper.like("DrugsName", entity.getDrugsName());
        }
        Page<Drugs> old = drugsService.page(page, wrapper);
        Page<Wrap> pageWrap = new Page<>();
        List<Wrap> wraps = new ArrayList<>();
        List<Drugs> object = old.getRecords();
        for (Drugs item : object) {
            wraps.add(convert(item));
        }
        pageWrap.setRecords(wraps);
        PageUtil pageUtil = new PageUtil();
        pageUtil.setPage(pageWrap, old);
        return pageWrap;
    }

    /**
     * 导出药品信息列表
     */
    @ApiOperation(value = "导出药品信息列表")
    @PreAuthorize("@ss.hasPermi('system:Drugs:export')")
    @Log(title = "药品信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Drugs drugs) {
        LambdaQueryWrapper<Drugs> queryWrapper = new LambdaQueryWrapper<>();
        List<Drugs> list = drugsService.list(queryWrapper);
        ExcelUtil<Drugs> util = new ExcelUtil<Drugs>(Drugs.class);
        util.exportExcel(response, list, "药品信息数据");
    }


    /**
     * 获取药品信息详细信息
     */
    @ApiOperation(value = "获取药品信息详细信息")
    @PreAuthorize("@ss.hasPermi('system:Drugs:query')")
    @GetMapping(value = "/Information")
    public AjaxResult getInfo(@RequestParam("Id") Long id) {
        return success(drugsService.getById(id));
    }

    /**
     * 新增药品信息
     */
    @ApiOperation(value = "新增药品信息")
    @PreAuthorize("@ss.hasPermi('system:Drugs:add')")
    @Log(title = "药品信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Drugs drugs) {
        return toAjax(drugsService.save(drugs));
    }

    /**
     * 修改药品信息
     */
    @ApiOperation(value = "修改药品信息")
    @PreAuthorize("@ss.hasPermi('system:Drugs:edit')")
    @Log(title = "药品信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Drugs drugs) {
        return toAjax(drugsService.updateById(drugs));
    }


    /**
     * 删除药品信息
     */
    @DeleteMapping("/remove/{Ids}")
    public AjaxResult remove(@PathVariable Long[] Ids) {
        System.out.println(Ids);
        return toAjax(drugsService.removeBatchByIds(Arrays.asList(Ids)));
    }
}
